package com.example.demo.practice.area;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.NormalExpandableListAdapter;
import com.example.demo.practice.tool.ServerHttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by Declan_Wang on 2018/3/12.
 */

public class Area extends Fragment{
    public ServerHttp serverHttp;
    private List<String> groupData;
    private ListView area;
    private TextView areas;
    private Button street;
    private TextView now;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_area, null);
    }

    @Override
    public void onAttach(Activity activity) {
// TODO Auto-generated method stub
        super.onAttach(activity);
        if (activity instanceof AreaLists) {
            AreaLists mainActivity = (AreaLists) activity;
            areas = (TextView) mainActivity.findViewById(R.id.areas);//获取父activity的textview控件
            street = (Button) mainActivity.findViewById(R.id.street_btn);
            now = (TextView) mainActivity.findViewById(R.id.now);

            serverHttp = new ServerHttp(handler);
            serverHttp.setUrl("district");
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        area = (ListView) getActivity().findViewById(R.id.area);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            JSONArray jsonArray = serverHttp.getData();
            groupData = new ArrayList<String>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    String adName = json.getString("adname");
                    if (groupData.contains("\t\t" + adName)) {
                    } else {
                        groupData.add("\t\t" + adName);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            ArrayAdapter<String> Adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item1,groupData);
            area.setAdapter(Adapter);
            area.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String getObject = groupData.get(position).trim();   //通过position获取所点击的对象
                    areas.setText(getObject);
                    now.setText(getObject);
                    street.callOnClick();
                }
            });
            serverHttp.clear();
            super.handleMessage(msg);
        }
    };
}
